package com.example.hetong_system.repository;

import com.example.hetong_system.model.Clause;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface ClauseRepository extends JpaRepository<Clause, Long> {
    // 通过合同ID查询关联的条款（命名规则方式）
//    List<Clause> findByContractClausesContractId(Long contractId);

    // 查询未关联到指定合同的条款
    List<Clause> findByContractClausesContractIdIsNull();


    // 查询合同关联的条款
    @Query("SELECT c FROM Clause c " +
            "JOIN c.contractClauses cc " +
            "WHERE cc.contract.id = :contractId")
    List<Clause> findByContractClausesContractId(@Param("contractId") Long contractId);

    // 查询未关联到指定合同的条款（分页）
    @Query("SELECT c FROM Clause c " +
            "WHERE c NOT IN (SELECT cc.clause FROM ContractClause cc WHERE cc.contract.id = :contractId)")
    Page<Clause> findUnassociatedClauses(@Param("contractId") Long contractId, Pageable pageable);
}

